<style scoped>
.split {
    height: calc(100vh - 140px);
    background: #ffffff;
    border: 1px solid #dcdee2;
}

.split-pane {
    padding: 10px;
}

/deep/.ivu-table-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}

/deep/.ivu-table-fixed-header thead tr th {
    background: #2db7f5 !important;
    font-size: 15px;
    color: #211f1f;
}
</style>
<template>
    <div class="split">
        <div class="split-pane">
            <Card style="margin-bottom: 10px">
                <Form inline class="form-search">
                    <FormItem>
                        <Input v-model="keyword" placeholder="名称" style="width: 200px" />
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="
                            page = 1;
                        SearchProduct();" icon="md-search">查询</Button>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="ExportProduct();" icon="md-search">导出</Button>
                    </FormItem>
                </Form>
            </Card>
            <Table ref="table" style="margin-bottom: 10px" border :loading="loadingTable" :columns="tableTitle" :data="tableData" :height="tableHeight"></Table>
            <Page :total="total" :current="page" :page-size="size" :page-size-opts="[10, 50, 100]" @on-change="(s) => {
                page = s; SearchProduct();
            }" @on-page-size-change="(p) => { size = p; page = 1; SearchProduct(); }" show-total show-sizer />

            <Modal :footer-hide="true" :width="1000" draggable v-model="modalData" title="变更记录" :mask-closable="false">
                <productlog :Id="tempDataId" v-if="modalData" />
            </Modal>
        </div>
    </div>
</template>
<script>

import { SearchProduct, GetTreeProductType, DeleteProduct, SearchUnit, ExportProduct } from '@/api/product'

import { InitProductTypeTree } from '@/libs/tools'
import productlog from '../product/productlog.vue'
export default {
    name: 'product',
    components: {
        productlog
    },
    data() {
        return {
            split1: 0,
            total: 0,
            page: 1,
            size: 10,
            tableHeight: 450,
            keyword: '',
            typeid: 0,
            ruleValidate: {
                TypeId: [
                    {
                        type: 'number',
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ],
                Name: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: 'blur'
                    }
                ]
            },
            loading: false,
            loadingTable: false,
            modalData: false,
            tempDataId: 0,
            typeList: [],
            treeData: [],
            selectTreeData: [],
            tableTitle: [
                {
                    type: 'index',
                    title: '序号',
                    align: 'center',
                    width: 60,
                    // render: (h, params) => {
                    //     return h('span', params.index + (this.page - 1) * this.size + 1)
                    // }
                    indexMethod: (params) => {
                        return params._index + (this.page - 1) * this.size + 1;
                    }
                },
                {
                    title: '记录',
                    align: 'center',
                    resizable: true,
                    width: 80,
                    render: (h, params) => {
                        return h(
                            'Button',
                            {
                                props: {
                                    type: 'success',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.tempDataId = params.row.Id
                                        this.modalData = true
                                    }
                                }
                            },
                            '查看'
                        )
                    }
                }, {
                    key: "Name",
                    title: "名称",
                    minWidth: 150,
                    resizable: true,
                    align: "left",
                },
                {
                    key: "Specification",
                    title: "规格",
                    minWidth: 150,
                    resizable: true,
                    align: "left",
                },
                {
                    key: "Manufacturer",
                    title: "生产厂家",
                    minWidth: 150,
                    resizable: true,
                    align: "left",
                },
                {
                    key: "Formulations",
                    title: "剂型",
                    minWidth: 100,
                    resizable: true,
                    align: "center",
                },
                {
                    key: "Price",
                    title: "价格",
                    minWidth: 100,
                    resizable: true,
                    align: "center",
                },
                {
                    key: "ExpirationDate",
                    title: "有效期至",
                    minWidth: 120,
                    align: "center",
                    render: (h, params) => {
                        return h('span', {}, this.dayjs(params.row.ExpirationDate).format('YYYY-MM-DD'))
                    }
                },
                {
                    key: 'Num',
                    title: '库存',
                    minWidth: 100,
                    resizable: true,
                    align: 'center'
                },
                {
                    key: 'Unit',
                    title: '单位',
                    minWidth: 100,
                    resizable: true,
                    align: 'center'
                }
            ],
            tableData: [],
            unitList: []
        }
    },
    computed: {},
    methods: {
        changeTree(a, b) {
            this.typeid = b.id
            this.SearchProduct()
        },

        SearchProduct() {
            let vm = this
            vm.loadingTable = true
            var reqData = {
                keyword: vm.keyword,
                typeid: vm.typeid,
                page: vm.page,
                size: vm.size,
            }
            SearchProduct(reqData).then((res) => {
                vm.loadingTable = false
                var data = res.data
                if (data.StatusCode == 200) {
                    vm.tableData = data.Data.Items
                    vm.total = data.Data.TotalCount
                } else {
                    vm.$Notice.error({
                        title: data.Errors
                    })
                }
            })
        },
        ExportProduct() {
            let vm = this
            var reqData = {
                keyword: vm.keyword,
                typeid: vm.typeid,
            };
            ExportProduct(reqData).then((res) => {
                this.downloadfile(res)
            }).catch((err) => {
                vm.$Message.error({ title: '导出错误', content: err })
            })
        },
        downloadfile(res) {
            var blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
            var contentDisposition = res.headers['content-disposition']
            var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
            var filename = "药品库存.xlsx"
            if (contentDisposition != undefined) {
                var result = patt.exec(contentDisposition)
                filename = result[1]
            }
            var downloadElement = document.createElement('a')
            var href = window.URL.createObjectURL(blob) // 创建下载的链接
            var reg = /^["](.*)["]$/g
            downloadElement.style.display = 'none'
            downloadElement.href = href
            downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
            document.body.appendChild(downloadElement)
            downloadElement.click() // 点击下载
            document.body.removeChild(downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL(href)
        },
        GetTreeProductType() {
            let vm = this
            GetTreeProductType(0).then((res) => {
                const data = res.data
                if (data.StatusCode == 200) {
                    this.selectTreeData = InitProductTypeTree(data.Data)
                    this.treeData = [
                        {
                            id: 0,
                            label: '全部',
                            title: '全部',
                            value: 0,
                            expand: true,
                            children: this.selectTreeData
                        }

                    ]
                } else {
                    vm.$Message.error({ title: '提示', content: data.Errors })
                }
            })
        },
        DeleteProduct(id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确定要删除吗？',
                onOk: () => {
                    DeleteProduct(id).then((res) => {
                        const data = res.data
                        if (data.StatusCode == 200) {
                            this.SearchProduct()
                            this.$Message.success({ title: '提示', content: '删除成功' })
                        } else {
                            this.$Message.error({ title: '提示', content: data.Errors })
                        }
                    })
                },
                onCancel: () => { }
            })
        },
        SearchUnit() {
            let vm = this
            var reqData = {

            }
            SearchUnit(reqData).then((res) => {
                vm.loadingTable = false
                var data = res.data
                if (data.StatusCode == 200) {
                    vm.unitList = data.Data.map(m => { return m.Name })
                } else {
                    vm.$Notice.error({
                        title: data.Errors
                    })
                }
            })
        },

        AddData() {
            this.tempData = {

            }
            this.modalData = true
        }
    },

    mounted() {
        this.tableHeight = window.innerHeight - 300
        //this.GetTreeProductType()
        this.SearchProduct()
        this.SearchUnit()
    }
}
</script>
